﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormQQOnLineSumary.cs" company="">
//   
// </copyright>
// <summary>
//   The form qq on line sumary.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace NetworkSupervisor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    using DevExpress.Data.Filtering;
    using DevExpress.Xpo;
    using DevExpress.XtraGrid.Views.Base;
    using DevExpress.XtraGrid.Views.Grid;

    using NetworkSupervisor.Helper;
    using NetworkSupervisor.Model.NetworkSupervisor;

    /// <summary>The form qq on line sumary.</summary>
    public partial class FormQQOnLineSumary : DevExpress.XtraEditors.XtraForm
    {
        #region Fields

        /// <summary>The close it.</summary>
        private bool closeIt = false;

        /// <summary>The loading.</summary>
        private FormLoadProgress loading = null;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="FormQQOnLineSumary"/> class.</summary>
        public FormQQOnLineSumary()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>The form qq on line sumary_ form closing.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void FormQQOnLineSumary_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.closeIt = true;
        }

        /// <summary>The form qq on line sumary_ load.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void FormQQOnLineSumary_Load(object sender, EventArgs e)
        {
            this.txtStartTime.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            this.txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>The form qq on line sumary_ shown.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void FormQQOnLineSumary_Shown(object sender, EventArgs e)
        {
            this.loading = new FormLoadProgress();
            this.loading.mySetInfo("正在加载数据，请等候...");
            this.loading.mySetProgressMaximum(this.XPCQQ列表.Count);
            this.BeginInvoke(new MethodInvoker(() => this.loading.ShowDialog()));
            System.Threading.ThreadPool.QueueUserWorkItem(this.callBack);
        }

        /// <summary>The btn export_ click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            GridExportHelper.ExportGridToExcel(this.gridControl1, "在线情况统计列表");
        }

        /// <summary>The btn exprot one on line_ click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void btnExprotOneOnLine_Click(object sender, EventArgs e)
        {
            taQQ列表 item = this.gridView1.GetFocusedRow() as taQQ列表;
            if (item != null)
            {
                GridExportHelper.ExportGridToExcel(this.gridControl2, string.Format("个人在线情况列表[{0}][{1}]", item.QQ, item.标识号));
            }
        }

        /// <summary>The btn filter_ click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void btnFilter_Click(object sender, EventArgs e)
        {
            DateTime startTime = Convert.ToDateTime(this.txtStartTime.Text);
            DateTime endTime = Convert.ToDateTime(this.txtEndTime.Text);
            BetweenOperator betweenOperator = new BetweenOperator("在线时间", startTime, endTime);
            BinaryOperator binaryOperator = new BinaryOperator("在线状态", true);
            GroupOperator groupOperator = new GroupOperator(new CriteriaOperator[] { betweenOperator, binaryOperator });
            XPCollection<taQQ在线纪录> collection = new XPCollection<taQQ在线纪录>(this.session1, groupOperator, null);
            IEnumerable<string> onLineQqList = collection.Select(o => o.QQ).Distinct().ToArray();

            IList<taQQ列表> list = this.XPCQQ列表.Cast<taQQ列表>().Where(item => onLineQqList.Contains(item.QQ)).ToList();

            this.gridControl1.DataSource = list;
        }

        /// <summary>The btn show all_ click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void btnShowAll_Click(object sender, EventArgs e)
        {
            this.gridControl1.DataSource = this.XPCQQ列表;
        }

        /// <summary>The call back.</summary>
        /// <param name="state">The state.</param>
        private void callBack(object state)
        {
            List<taQQ列表> list = this.XPCQQ列表.Cast<taQQ列表>().ToList();
            int i = 0;
            foreach (taQQ列表 item in list)
            {
                if (this.closeIt)
                {
                    return;
                }

                try
                {
                    this.UIThread(
                        () =>
                            {
                                if (item != null)
                                {
                                    item.Set在线次数();
                                }
                            });
                    this.UIThread(() => this.loading.mySetProgressPosition(++i));
                }
                catch (Exception ex)
                {
                }
            }

            this.UIThread(() => this.loading.myClose());
        }

        /// <summary>The grid view 1_ focused row changed.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (this.gridView1.RowCount < 1)
            {
                return;
            }

            string qq = this.gridView1.GetFocusedRowCellDisplayText("QQ");
            this.XPC在线记录.CriteriaString = string.Format("QQ='{0}'", qq);

            this.chartControl1.DataSource = this.XPC在线记录.Cast<taQQ在线纪录>().OrderByDescending(o => o.在线时间).Take(12).ToList();
        }

        /// <summary>The grid view 2_ custom draw row indicator.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void gridView2_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        #endregion
    }
}